package com.automobile.service.impl;

import com.automobile.dao.CollectDao;
import com.automobile.domain.Announce;
import com.automobile.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类的作用
 *
 * @author liujiamiao
 * @email 2544296058@qq.com
 * @date 2020-03-24 16:12
 */
@CacheConfig(cacheManager = "redisCacheManager")
@Service("collectService")
public class CollectServiceImpl implements CollectService {

    @Autowired
    private CollectDao collectDao;

    /**
     * 查询用户的收藏商品，包含announce和smart
     * @param userId
     * @return
     */
//    @Cacheable(value = {"collect"},key = "#userId")
// 这里取消缓存的原因是，收藏查询次数较少，反而我们在查看商品的收藏/取消收藏更多，
// 这样的话我们需要每次都更新该缓存 ，效率反而下降了
    @Override
    public Map<String,Object> findCollectByUserId(int userId) {
        Map<String,Object> map = new HashMap<>();
        map.put("announce",collectDao.findCollectByUserId(userId));
        map.put("smart",collectDao.findCollectByUserId2(userId));
        return map;
    }








    /**
     * 查询该用户是否收藏了announce商品
     * @param userId
     * @param announceId
     * @return
     */
    @Cacheable(key = "#userId+'-'+#announceId",value = {"announceCollect"})
    @Override
    public int findCollect(int userId, int announceId) {
        return collectDao.findCollect(userId,announceId);
    }

    /**
     * 取消该用户收藏announce商品,该方法是在商品收藏中删除的
     * @param userId
     * @param announceId
     */
    @CacheEvict(key = "#userId+'-'+#announceId",value = {"announceCollect"})
    @Override
    public void deleteCollect(int userId, int announceId) {
        collectDao.deleteCollect(userId,announceId);
    }

    /**
     * 该用户收藏了announce商品
     * @param userId
     * @param announceId
     */
    @Override
    public void save(int userId, int announceId) {
        collectDao.save(userId,announceId);
    }

    /**
     * 如果没收藏则是收藏
     * 如果已经收藏，则是取消收藏
     * @param userId
     * @param announceId
     * @return
     */
    @CachePut(key = "#userId+'-'+#announceId",value = {"announceCollect"})
    @Override
    public int taggleAnnounce(int userId, int announceId) {
        int flag = findCollect(userId, announceId);
        if(flag==0) {
            save(userId, announceId);
            return 1;
        }else {
            deleteCollect(userId, announceId);
            return 0;
        }
    }











    @Override
    public void save2(int userId, int smartId) {
        collectDao.save2(userId,smartId);
    }

    /**
     * 该用户取消smart商品收藏,该方法是在商品收藏中删除的
     * @param userId
     * @param smartId
     */
    @CacheEvict(key = "#userId+'-'+#smartId",value = {"smartIdCollect"})
    @Override
    public void deleteCollect2(int userId, int smartId) {
        collectDao.deleteCollect2(userId,smartId);
    }

    /**
     * 查询该用户是否收藏了smart商品
     * @param userId
     * @param smartId
     * @return
     */
    @Cacheable(key = "#userId+'-'+#smartId",value = {"smartIdCollect"})
    @Override
    public int findCollect2(int userId, int smartId) {
        return collectDao.findCollect2(userId,smartId);
    }

    /**
     * 如果没收藏则是收藏
     * 如果已经收藏，则是取消收藏
     * @param userId
     * @param smartId
     * @return
     */
    @CachePut(key = "#userId+'-'+#smartId",value = {"smartIdCollect"})
    @Override
    public int taggleSmart(int userId, int smartId) {
        int flag = findCollect2(userId, smartId);
        if(flag==0) {
            save2(userId, smartId);
            return 1;
        }else {
            deleteCollect2(userId, smartId);
            return 0;
        }
    }
}
